// pages/service_choice.js
const app = getApp()
import Toast from '@vant/weapp/toast/toast';
var reqHelp = require('../../utils/reqHelp.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        cardIccid:'',//卡号
        dataList:[],
        list2:[],
        mealshow:false,// 是否显示弹窗框
        title:'温馨提示',
        showAlert:false,
        warningMessage:'',
        objCard:{},
        checkRules:true
       
    },
    onCheckRules(){
      const that =this;
      this.setData({
        checkRules: !that.data.checkRules
      })
   
    },
    onLoad(options){
        this.selectComponent("#loading").show()
        var cardId = options.cardId; //"35904";//
        var that = this;
        app.getLogUser(function (uInfo) {
            const {
                userId,
                userAvatar,
                userOpenId
              } = uInfo;
              that.setData({
                uId: userId,
                uAvatar: userAvatar,
                uOpenId: userOpenId,
                cardId: cardId
              })
          that.getData(cardId)
          that.selectComponent("#loading").hide() 
        });
    },
    getData(cardId){
      const that =this
      wx.request({
        url: app.globalData.apiUrlNew + 'Mini/BalUpdated.ashx?action=initServiceChange',
        data: {
          cardId: cardId
        },
        dataType: 'json',
        header: {
          'content-type': 'application/json' // 默认值
        },
        success(res) {
          var respd = res.data;
          console.log(respd)
          if (respd.getNum == 0) {
            that.setData({
              dataList: respd.dtMeal,
              oldMealId: respd.mealId,
              oldMealName: respd.mealName,
              dateStart: respd.dateEffect,
              cardIccid: respd.cardIccid,
              objCard: respd.objCard
            });
          } else {
              Toast(respd.getMsg)
          }
        },
        fail(resfail) {
          Toast(resfail.data)
        }
      })
  },
    //关闭套餐详情
    closeMeal(){
        this.setData({ mealshow: false });
    },
    //选中套餐
    onSelect(e){
        this.setData({ mealshow: true });
        const mealId = e.currentTarget.dataset.item.pro_id
        this.setData({
           mealId:mealId,
        })
       var that = this;
       reqHelp.requestByJson(app.globalData.apiUrl+'Mini/BalOrder.ashx?action=7', {
         cardId: that.data.cardId,
         mealId: mealId
       }).then(function (res) {
          console.log(res)
        if (res.getNum == 0) {
          let votArray = res.getMsg.voucher.split('^')
          that.setData({
            mealName: res.getMsg.mealName,
            mealPrice: res.getMsg.mealMarketPrice,
            mealTime: '到期日:' + res.getMsg.dateEnd,
            votList: {
              votGroup: votArray[0],
              vouName: votArray[1],
              vouDesc: votArray[2],
              vouMoneyPrice: votArray[3],
            },
            lastPay: res.getMsg.mealMarketPrice - that.data.cardRestMoney - votArray[3]
          });
        } else {
          that.setData({
            mealName: res.getMsg,
          });
        }
      });
    },
    onPay(){
      if(!this.data.checkRules){
        Toast('请同意套餐协议')
        return 
      }
        const that = this;
        wx.request({
            url: app.globalData.apiUrl + 'Mini/BalCard.ashx?action=5',
            data: {
              cardId: that.data.cardId,
              userId: that.data.userId,
              oldMealId: that.data.oldMealId,
              newMealId: that.data.mealId,
            },
            dataType: 'json',
            header: {
              'content-type': 'application/json' // 默认值
            },
            success(resp) {
                console.log(resp)
              var respd = resp.data;
              if (respd.getNum == 0) {
                  that.setData({
                    showAlert:true,
                    warningMessage:'变更成功,下个周期生效',
                    mealshow:false
                })
              } else {
                that.setData({
                    showAlert:true,
                    warningMessage:"T" + respd.getNum + respd.getMsg,
                    mealshow:false
                })
            }
            },
            fail(resfail) {
                console.log.log(resfail)
                that.setData({
                    showAlert:true,
                    warningMessage:resfail.data,
                    mealshow:false
                })
            }
        }) 
    }
})